package cn.tianyigps.tianyigpsaddress.assgin;

import cn.tianyigps.tianyigpsaddress.bean.EquipmentDTO;
import cn.tianyigps.tianyigpspublic.bean.EquipmentModel;
import cn.tianyigps.tianyigpspublic.util.MapTransformUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.data.geo.Point;
import org.springframework.util.StringUtils;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.Optional;
import java.util.stream.Collectors;

/**
 * Created with IDEA
 * author:guibin.cui
 * Date:2018/7/13
 * Time:20:44
 * 赋值工具
 */

public class AssginUtil {

    private static final String  BAIDU = "bd09";
    private static final String  GCJ02 = "gcj02";
    private static final String  FILTER = "filter";
    private static final String  ALL = "all";
    private static final String  GPS = "00";
    /**
     * 获取总公里数
     * @param equipmentCheckModels
     */
    public  static  List<EquipmentDTO> assginEquipment(List<EquipmentModel> equipmentCheckModels, Map<String,Object> map) {

        // 过滤没有经纬度的数据
        List<EquipmentModel> equipmentCheckModelListResult = equipmentCheckModels.stream().filter(equipmentCheckModel ->
                (Optional.ofNullable(equipmentCheckModel.getGpsLongitude()).isPresent() && Optional.ofNullable(equipmentCheckModel.getGpsLatitude()).isPresent()))
                .collect(Collectors.toList());
        List<EquipmentModel> equipmentCheckModelList = equipmentCheckModelListResult.stream().filter(equipmentCheckModel -> (equipmentCheckModel.getGpsLatitude() <= 90.00)).collect(Collectors.toList());
        // 计算总公里数
        Double totalKms = 0.0;
        Double totalSpeed = 0.0;
        // 平均速度
        Double avgSpeed = 0.0;
        // 有速度个数
        Integer count = 0;
        /**=================================== mapType ==========================================*/
        List<EquipmentDTO> equipmentDTOS = new ArrayList<>();
        for (int i = 0; i < equipmentCheckModelList.size(); i++){
            EquipmentModel equipmentStart;
            if (i == 0){
                equipmentStart = equipmentCheckModelList.get(i);
            }else{
                equipmentStart =equipmentCheckModelList.get(i - 1);
            }
            EquipmentDTO equipmentDTO = new EquipmentDTO();
            EquipmentModel equipmentEnd = equipmentCheckModelList.get(i);
            map.put("equipmentId",equipmentEnd.getEquipmentId());
            BeanUtils.copyProperties(equipmentEnd,equipmentDTO);
            //百度系 BD09
            String mapType = (String) map.get("mapType");
            if (StringUtils.isEmpty(mapType))
                map.put("mapType",GCJ02);
            if (BAIDU.equals(map.get("mapType"))){
                Point point = MapTransformUtils.gcj02tobd09(equipmentEnd.getGpsLongitude(), equipmentEnd.getGpsLatitude());
                equipmentDTO.setGpsLongitude(point.getX());
                equipmentDTO.setGpsLatitude(point.getY());
            }
            //其他默认就是gcj02
            equipmentDTOS.add(equipmentDTO);
            if (!StringUtils.isEmpty(equipmentStart.getPowerType()))
                map.put("powerType",equipmentStart.getPowerType());

            /**=================================== speed and kms ==========================================*/

            if (Optional.ofNullable(equipmentStart.getSpeed()).isPresent() && 0 < equipmentStart.getSpeed() ) {
                totalSpeed += equipmentStart.getSpeed();
                count ++;
            }
            totalKms += LocationUtils.calcDistance(equipmentStart.getGpsLongitude(), equipmentStart.getGpsLatitude(), equipmentEnd.getGpsLongitude(), equipmentEnd.getGpsLatitude());
        }
        if(count > 0)
            avgSpeed = totalSpeed/count;
        map.put("totalKms",totalKms);
        map.put("avgSpeed",avgSpeed);
        // 赋值
        // 筛选GPS 定位
        String filter = (String) map.get("filter");
        if (StringUtils.isEmpty(filter))
             map.put("filter",ALL);
        if (FILTER.equals(map.get(filter)))
            equipmentDTOS = equipmentDTOS.stream().filter(equipmentDTO1 -> GPS.equals(equipmentDTO1.getLocationType())).collect(Collectors.toList());
        return equipmentDTOS;
    }
}
